package com.yy.netwrok.request;

import java.io.File;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;

public class CommonRequest {

    private static final MediaType FILE_TYPE = MediaType.parse("application/octet-stream");
    private static final String CONTENT_DiSPOSITION = "content-Disposition";

    public static Request createPostRequest(String url, RequestParams params) {
        return createPostRequest(url, params, null);
    }

    public static Request createPostRequest(String url, RequestParams params,
                                            RequestParams headers) {
        FormBody.Builder mFormBody = new FormBody.Builder();
        if (params != null) {
            for (Map.Entry<String, String> entry : params.urlParams.entrySet()) {
                mFormBody.add(entry.getKey(), entry.getValue());
            }
        }
        Headers.Builder mHeader = new Headers.Builder();
        if (headers != null) {
            for (Map.Entry<String, String> entry : headers.urlParams.entrySet()) {
                mHeader.add(entry.getKey(), entry.getValue());
            }
        }
        Request mRequest = new Request.Builder()
                .url(url).headers(mHeader.build())
                .post(mFormBody.build())
                .build();
        return mRequest;
    }

    public static Request createGetRequest(String url, RequestParams params) {
        return createGetRequest(url, params, null);
    }

    public static Request createGetRequest(String url, RequestParams params,
                                           RequestParams headers) {
        StringBuilder builder = new StringBuilder(url).append("?");
        if (params != null) {
            for (Map.Entry<String, String> entry : params.urlParams.entrySet()) {
                builder.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
        }
        Headers.Builder mHeader = new Headers.Builder();
        if (headers != null) {
            for (Map.Entry<String, String> entry : headers.urlParams.entrySet()) {
                mHeader.add(entry.getKey(), entry.getValue());
            }
        }
        return new Request.Builder().url(url.substring(0, url.length() - 1))
                .headers(mHeader.build()).get().build();
    }

    public static Request createMultiPostRequest(String url, RequestParams params) {
        MultipartBody.Builder builder = new MultipartBody.Builder();
        builder.setType(MultipartBody.FORM);
        if (params != null) {
            for (Map.Entry<String, Object> entry : params.fileParams.entrySet()) {
                if (entry.getValue() instanceof File) {
                    builder.addPart(Headers.of(CONTENT_DiSPOSITION,
                            "form-data; name=\"" + entry.getKey() + "\""),
                            RequestBody.create(FILE_TYPE, (File) entry.getValue()));
                } else if (entry.getValue() instanceof String) {
                    builder.addPart(Headers.of(CONTENT_DiSPOSITION,
                            "form-data; name=\"" + entry.getKey() + "\""), RequestBody.create(null
                            , (String) entry.getValue()));
                }
            }
        }
        return new Request.Builder().url(url).post(builder.build()).build();
    }
}